<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let arr = [1, 2, [3, 4], [5, [6, 7]]];
            console.log(arr.flat(1)); // [1, 2, 3, 4, 5, [6,7]]
            console.log(arr.flat(2)); // [1, 2, 3, 4, 5, 6, 7]

            // console.dir(Array.prototype);
            Array.prototype.myFlat = function (depth) {
                // 确定数组返回的条件，如果不是数组或者已经到了拍平的层数
                if (!Array.isArray(this) || depth <= 0) {
                    return arr;
                }
                return arr.reduce((pre, cur) => {
                    return pre.concat(Array.isArray(cur) ? cur.flat(depth - 1) : cur);
                }, []);
            };
            console.log(arr.myFlat(1)); // [1, 2, 3, 4, 5, [6,7]]
            console.log(arr.myFlat(2)); // [1, 2, 3, 4, 5, 6, 7]
        </script>
    </body>
</html>
